﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Data;
using DevExpress.Utils.Menu;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_ORDER.BizLogic;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_ORDER.OrderObject.Forms;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;

namespace ERP_RORZE_ORDER.OrderObject.UserControls
{
    public partial class ucReportGrid : DevExpress.XtraEditors.XtraUserControl
    {
        ReportHelper helper = new ReportHelper();
        private DataTable dtMain = new DataTable();
        public GridView GridView;
        DXPopupMenu menu;
        private int reportNameParent;
        private string captionParent;

        #region Handle methods
        public ucReportGrid(int reportName, string caption)
        {
            InitializeComponent();
            InitPopupMenu();
            this.reportNameParent = reportName;
            this.captionParent = caption;
            BindDatatoGrid(reportName, caption);
            gridView1.BestFitColumns();
            this.dropbtExport.DropDownControl = menu;
        }

        private void menuHideCols_Click(object sender, EventArgs e)
        {
            
        }

        private void menuShowAllCols_Click(object sender, EventArgs e)
        {

        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            BindDatatoGrid(reportNameParent, captionParent);
        }

        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridView1.RowCount != 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }

        private void gridControl1_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
            }
        }
        #endregion


        #region Private methods

        private void BindDatatoGrid(int reportName, string caption, int range = 1)
        {

            this.reportNameParent = reportName;
            this.captionParent = caption;
            switch (reportName)
            {
                case OrderConstants.Configuration.ReportName.QTheoDoiDatHang:

                    dtMain = helper.GetAll(OrderConstants.StoreProcedure.Report.QTheoDoiDatHang);
                    gridControl1.DataSource = dtMain;
                    gridView1.PopulateColumns();


                    GridColumn colSoLuongthieu1 = gridView1.Columns.ColumnByFieldName("Số hàng thiếu");
                    colSoLuongthieu1.DisplayFormat.FormatType = FormatType.Numeric;
                    colSoLuongthieu1.DisplayFormat.FormatString = "n0";

                    GridColumn colSoluongve1 = gridView1.Columns.ColumnByFieldName("Số lượng hàng về");
                    colSoluongve1.DisplayFormat.FormatType = FormatType.Numeric;
                    colSoluongve1.DisplayFormat.FormatString = "n0";

                    GridColumn colSLDathang1 = gridView1.Columns.ColumnByFieldName("Số lương đặt hàng");
                    colSLDathang1.DisplayFormat.FormatType = FormatType.Numeric;
                    colSLDathang1.DisplayFormat.FormatString = "n0";

                    gridView1.Columns["Date"].SortOrder = ColumnSortOrder.Descending;

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.BestFitColumns();
                    gridView1.ViewCaption = caption;
                    this.GridView = gridView1;
                    break;

                case OrderConstants.Configuration.ReportName.QHangchuave:

                    dtMain = helper.GetAll(OrderConstants.StoreProcedure.Report.QHangchuave);
                    gridControl1.DataSource = dtMain;
                    gridView1.PopulateColumns();

                    GridColumn colSoLuongthieu = gridView1.Columns.ColumnByFieldName("Số hàng thiếu");
                    colSoLuongthieu.DisplayFormat.FormatType = FormatType.Numeric;
                    colSoLuongthieu.DisplayFormat.FormatString = "n0";

                    GridColumn colSoluongve = gridView1.Columns.ColumnByFieldName("Số lượng hàng về");
                    colSoluongve.DisplayFormat.FormatType = FormatType.Numeric;
                    colSoluongve.DisplayFormat.FormatString = "n0";

                    GridColumn colSLDathang = gridView1.Columns.ColumnByFieldName("Số lương đặt hàng");
                    colSLDathang.DisplayFormat.FormatType = FormatType.Numeric;
                    colSLDathang.DisplayFormat.FormatString = "n0";

                    GridColumn columnPrice2 = gridView1.Columns.ColumnByFieldName("Price");
                    columnPrice2.DisplayFormat.FormatType = FormatType.Numeric;
                    columnPrice2.DisplayFormat.FormatString = "n2";

                    GridColumn columnExchangeRate2 = gridView1.Columns.ColumnByFieldName("ExchangeRate");
                    columnExchangeRate2.DisplayFormat.FormatType = FormatType.Numeric;
                    columnExchangeRate2.DisplayFormat.FormatString = "n2";

                    GridColumn columnAmount2 = gridView1.Columns.ColumnByFieldName("Amount(USD)");
                    columnAmount2.DisplayFormat.FormatType = FormatType.Numeric;
                    columnAmount2.DisplayFormat.FormatString = "n2";

                    gridView1.Columns["Date"].SortOrder = ColumnSortOrder.Descending;

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.BestFitColumns();
                    gridView1.ViewCaption = caption;
                    this.GridView = gridView1;
                    break;
                case OrderConstants.Configuration.ReportName.QDatHangDetails:

                    dtMain = helper.GetAll(OrderConstants.StoreProcedure.Report.QDatHangDetails);
                    gridControl1.DataSource = dtMain;
                    gridView1.PopulateColumns();

                    GridColumn columnDate = gridView1.Columns.ColumnByFieldName("Date");
                    columnDate.DisplayFormat.FormatType = FormatType.DateTime;
                    columnDate.DisplayFormat.FormatString = "dd/MM/yyyy";

                    GridColumn colDeadline = gridView1.Columns.ColumnByFieldName("Deadline");
                    colDeadline.DisplayFormat.FormatType = FormatType.DateTime;
                    colDeadline.DisplayFormat.FormatString = "dd/MM/yyyy";

                    GridColumn columnQuantity = gridView1.Columns.ColumnByFieldName("Quantity");
                    columnQuantity.DisplayFormat.FormatType = FormatType.Numeric;
                    columnQuantity.DisplayFormat.FormatString = "n0";

                    GridColumn columnPrice = gridView1.Columns.ColumnByFieldName("Price");
                    columnPrice.DisplayFormat.FormatType = FormatType.Numeric;
                    columnPrice.DisplayFormat.FormatString = "n2";

                    GridColumn columnExchangeRate = gridView1.Columns.ColumnByFieldName("ExchangeRate");
                    columnExchangeRate.DisplayFormat.FormatType = FormatType.Numeric;
                    columnExchangeRate.DisplayFormat.FormatString = "n2";

                    GridColumn columnAmount = gridView1.Columns.ColumnByFieldName("Amount(USD)");
                    columnAmount.DisplayFormat.FormatType = FormatType.Numeric;
                    columnAmount.DisplayFormat.FormatString = "n2";

                    gridView1.Columns["Date"].SortOrder = ColumnSortOrder.Descending;

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.BestFitColumns();
                    gridView1.ViewCaption = caption;
                    this.GridView = gridView1;
                    break;
                case OrderConstants.Configuration.ReportName.QMonthlyThanhToan_hang:

                    dtMain = helper.GetAll(OrderConstants.StoreProcedure.Report.QMonthlyThanhToan_hang);
                    gridControl1.DataSource = dtMain;
                    gridView1.PopulateColumns();
                    foreach (GridColumn column in gridView1.Columns)
                    {
                        //if (column.FieldName != "GroupName" && column.FieldName != "Total Of Amount(USD)")
                        if (column.FieldName != "GroupName")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n2";
                        }
                    }

                    gridView1.Columns["GroupName"].SortOrder = ColumnSortOrder.Ascending;

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.BestFitColumns();
                    gridView1.ViewCaption = caption;
                    this.GridView = gridView1;
                    break;
                case OrderConstants.Configuration.ReportName.QMonthlyDatHangBophanDetails_Crosstab:

                    dtMain = helper.GetAll(OrderConstants.StoreProcedure.Report.QMonthlyDatHangBophanDetails_Crosstab);
                    gridControl1.DataSource = dtMain;
                    gridView1.PopulateColumns();
                    foreach (GridColumn column in gridView1.Columns)
                    {
                        //if (column.FieldName != "PGroupName" && column.FieldName != "Total Of Amount(USD)")
                        if (column.FieldName != "PGroupName")
                        {
                            column.DisplayFormat.FormatType = FormatType.Numeric;
                            column.DisplayFormat.FormatString = "n2";
                        }
                    }

                    gridView1.Columns["PGroupName"].SortOrder = ColumnSortOrder.Ascending;

                    gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.BestFitColumns();
                    gridView1.ViewCaption = caption;
                    this.GridView = gridView1;
                    break;

                default:
                    break;

            }

        }


        protected virtual void InitPopupMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemClickExportAll), null, null, null, null));
            menu.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemClickExportRows), null, null, null, null));

        }
        void OnItemClickExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 1, true);
        }
        void OnItemClickExportRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 0, true);
        }

        #endregion



    }
}
